﻿using MDK.Common;
using MDK.Model.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmSysConfig : BaseForm
    {
        private T_Systemsetting _set { get; set; }

        public frmSysConfig()
        {
            InitializeComponent();
        }

        public frmSysConfig(string _setId) : this()
        {
            try
            {

                PropertyHelper jsonWhere = new PropertyHelper();
                jsonWhere.Addproperty(CompareType.Equal, "Id", _setId);
                string url = string.Format("Systemsetting/Find?jsonWhere={0}", JsonConvert.SerializeObject(jsonWhere.propertyList));
                _set = WebAPIHelper.Get<T_Systemsetting>(url);
                if (_set == null)
                {
                    FastMessageBox.Error("无法获取用户信息！");
                    Close();
                    Dispose();
                    DialogResult = DialogResult.Abort;
                }
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
                Close();
                Dispose();
                DialogResult = DialogResult.Abort;
            }
            BingData();
        }


        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BingData()
        {
            txtKey.DataBindings.Add("Text", _set, "SysKey");
            txtValue.DataBindings.Add("Text", _set, "SysValue");
            cmdIs_Enable.DataBindings.Add("Text", _set, "Is_Enable_String");
            txtRemark.DataBindings.Add("Text", _set, "Remark");
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (_set == null)
                {
                    _set = new T_Systemsetting();
                    _set.Id = Guid.NewGuid().ToString("N");
                    _set.SysKey = txtKey.Text.Trim();
                    _set.SysValue = txtValue.Text.Trim();
                    _set.Is_Enable = cmdIs_Enable.SelectedIndex == 0;
                    _set.Remark = txtRemark.Text.Trim();
                    WebAPIHelper.Post<T_Systemsetting>("Systemsetting/Add", _set);
                }
                else
                {
                    _set.SysKey = txtKey.Text.Trim();
                    _set.SysValue = txtValue.Text.Trim();
                    _set.Is_Enable = cmdIs_Enable.SelectedIndex == 0;
                    _set.Remark = txtRemark.Text.Trim();
                    WebAPIHelper.Post<T_Systemsetting>("Systemsetting/Modify", _set);
                }
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            DialogResult = DialogResult.Cancel;
        }
    }
}
